package com.yangming.controller;

import com.alibaba.fastjson.JSONArray;
import com.yangming.common.base.BaseController;
import com.yangming.common.base.BaseResponseEnum;
import com.yangming.common.base.Page;
import com.yangming.common.utils.BackJsonBeanUtil;
import com.yangming.common.utils.RequestUtil;
import com.yangming.model.EmptyCartService;
import com.yangming.model.EmptyCartServiceRoute;
import com.yangming.model.User;
import com.yangming.service.IEmptyCartServiceService;
import com.yangming.service.IRecordsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 空板车服务Controller
 * @author PengShuiMing
 * @create 2018-09-07
 **/
@RestController
@RequestMapping("/service")
public class EmptyCartServiceController extends BaseController {
    private static final Logger logger =   LoggerFactory.getLogger(EmptyCartServiceController.class);
    @Autowired
    private IEmptyCartServiceService emptyCartServiceService;
    @Autowired
    private IRecordsService recordsService;
    /**
     * 发布空板车服务
     */

    @RequestMapping("/saveEmptyCartService.json")
    public Object saveEmptyCartService(EmptyCartService emptyCartService,String  routes) {
        //返回信息
        BaseResponseEnum responseEnum = BaseResponseEnum.ERROR;
        try {
            User user = this.getUser();
            emptyCartService.setUserId(user.getId());
            //会员用户设置权重
            if (user.getType()==2){
                emptyCartService.setWeight(1);
            }else {
                emptyCartService.setWeight(0);
            }
            //途径地点
            if(StringUtils.isNotBlank(routes)){
                List<EmptyCartServiceRoute> emptyCartServiceRoutes = JSONArray.parseArray(routes, EmptyCartServiceRoute.class);
                emptyCartService.setRoute(emptyCartServiceRoutes);
            }
            int result = emptyCartServiceService.saveEmptyCartService(emptyCartService);
            if(result>0){
                responseEnum = BaseResponseEnum.SUCCESS;
            }
        } catch (Exception e) {
            logger.error("[发布空板车服务]",e.getMessage(),e);
        }
        return BackJsonBeanUtil.getBackJsonBean(responseEnum,null);
    }

    /**
     * 分页查询空板车服务
     */
    @RequestMapping("/public/selectList.json")
    public Object selectList(Page page) {
        Map<String, Object> params = RequestUtil.getRequestParams(request);
        //返回信息
        BaseResponseEnum responseEnum = BaseResponseEnum.ERROR;
        Map<String, Object> resultMap = new HashMap<>();
        try {
            //查询是否过期
            params.put("endTime",new Date());
            resultMap = emptyCartServiceService.selectListByPage(params,page);
            responseEnum = BaseResponseEnum.SUCCESS;
        } catch (Exception e) {
            logger.error("[分页查询空板车服务]",e.getMessage(),e);
        }
        return BackJsonBeanUtil.getBackJsonBean(responseEnum,resultMap);
    }
    /**
     * 查询空板车服务详情
     */
    @RequestMapping("/public/selectOne.json")
    public Object selectOne(Integer id) {
        //返回信息
        BaseResponseEnum responseEnum = BaseResponseEnum.ERROR;
        EmptyCartService emptyCartService = new EmptyCartService();
        try {
            emptyCartService = emptyCartServiceService.selectOne(id);
            //过滤手机号码
            emptyCartService.setPhone(null);
            responseEnum = BaseResponseEnum.SUCCESS;
        } catch (Exception e) {
            logger.error("[查询空板车服务详情]",e.getMessage(),e);
        }
        return BackJsonBeanUtil.getBackJsonBean(responseEnum,emptyCartService);
    }
}
